package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderSettingListDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingListService.class)
@Transactional
public class OrderSettingListServiceImpl implements OrderSettingListService {
    @Autowired
    private OrderSettingListDao orderSettingListDao;
    @Autowired
    private MemberDao memberDao;



    /**
     * 预约信息分页查询
     *
     * @param
     */
    @Override
    public PageResult findOrdersPage(QueryPageBean queryPageBean) {
        //获取全部分页数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        String startDate = queryPageBean.getStartDate();
        String endDate = queryPageBean.getEndDate();
        String orderType = queryPageBean.getOrderType();
        String orderStatus = queryPageBean.getOrderStatus();
        //分页插件
        PageHelper.startPage(currentPage, pageSize);
        Page<Order> page = orderSettingListDao.selectByCondition(startDate, endDate, orderType, orderStatus, queryString);

        long total = page.getTotal();
        List<Order> rows = page.getResult();
        return new PageResult(total, rows);
    }



    /*添加预约信息,关联套餐信息*/
    @Override
    public void add(Order order, Integer setmealId) {
        String phoneNumber = order.getPhoneNumber();
        String memberName = order.getMemberName();
        Member member = new Member();
        Member byTelephone = memberDao.findByTelephone(phoneNumber);

        /*//3.防止重复预约
        if(member != null){
            Integer memberId = member.getId();
            int setmealId = Integer.parseInt((String)map.get("setmealId"));
            Order order = new Order(memberId, date, null, null, setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0){
                //已经完成了预约，不能重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }*/

        if(byTelephone == null){
            member.setPhoneNumber(phoneNumber);
            member.setName(memberName);
            memberDao.add(member);
        }
        Member byTelephone1 = memberDao.findByTelephone(phoneNumber);
        Integer memberId = byTelephone1.getId();
        order.setMemberId(memberId);
        order.setSetmealId(setmealId);
        orderSettingListDao.add(order);
    }

    /*根据id查询*/
    @Override
    public Map findById(Integer id) {
        return orderSettingListDao.findById(id);
    }
    @Override
    public Integer findSetmealIdByOrderId(Integer id) {
        Map map = orderSettingListDao.findSetmealIdByOrderId(id);
        Integer setmealId = (Integer) map.get("setmealId");
        return setmealId;
    }



    /*编辑*/
    @Override
    public void edit(Integer setmealId, Order order) {
        //根据id改会员昵称
        memberDao.updateNameById(order);
        //设置套餐的id
        order.setSetmealId(setmealId);
        //调用dao
        orderSettingListDao.update(order);
    }


    @Override
    public void delete(Integer id) {
        //删除
        orderSettingListDao.deleteById(id);
    }

    /*改变预约状态*/
    @Override
    public void changeStatus(Order order) {
        orderSettingListDao.changeStatus(order);
    }




}
